package com.itheima.Listtest;

import java.util.ArrayList;

//要求:查找用户的id,存在则返回下标,不存在返回-1.同时也返回ture和false
public class listTest7 {
    public static void main(String[] args) {
        //1.创建集合
        ArrayList<User> list = new ArrayList<>();

        //2.创建对象
        User u1 = new User("001","张三","123456");
        User u2 = new User("002","李四","12345678");
        User u3 = new User("003","王五","123456789");

        //3.把用户对象添加到集合中
        list.add(u1);
        list.add(u2);
        list.add(u3);

        //4.调用方法查看id是否存在
        System.out.println(checkUser(list, "001"));
        System.out.println(GetIndex(list, "001"));
    }

    public static boolean checkUser(ArrayList<User> list, String id){
        return GetIndex(list,"001")>=0;
    }

    public static int GetIndex(ArrayList<User> list, String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

}
